<?php
$cs = Yii::app()->getClientScript();
$cs->registerCSSFile(Yii::app()->request->baseUrl . '/css/mobile/booking_find_1.css');
$todayBegin = strtotime(date('Y-m-d',time()).' 00:00:00');
$todayEnd = $todayBegin + 60 * 60 * 24;

/*
 * 读取近日的预订数量
 */
$itemData1 = array(
    'paymentStatus' => BCart::Payment_Status_Paid,
    'paidTime' => '>='.($todayBegin-60*60*24*6),//获取提前7天的开始时间
    'groupBy' => 'paidTime'
);
$cart = new BWCart();
$view1 = $cart->getView(array('itemData' => $itemData1));
$theseDayBooking = array();
for($i=6; $i>=0; $i--) {
    $theseDayBooking[$i] = 0;
}
foreach ($view1 as $cartList) {
    if ($cartList instanceof BTreeList) {
        $paidTime = $cartList->getItem('paidTime')->value;
        switch ($paidTime) {
            case ($paidTime>=$todayBegin && $paidTime<=$todayEnd):
                $theseDayBooking[0] += 1;
                break;
            case ($paidTime>=$todayBegin-60*60*24*1 && $paidTime<=$todayEnd-60*60*24*1):
                $theseDayBooking[1] += 1;
                break;
            case ($paidTime>=$todayBegin-60*60*24*2 && $paidTime<=$todayEnd-60*60*24*2):
                $theseDayBooking[2] += 1;
                break;
            case ($paidTime>=$todayBegin-60*60*24*3 && $paidTime<=$todayEnd-60*60*24*3):
                $theseDayBooking[3] += 1;
                break;
            case ($paidTime>=$todayBegin-60*60*24*4 && $paidTime<=$todayEnd-60*60*24*4):
                $theseDayBooking[4] += 1;
                break;
            case ($paidTime>=$todayBegin-60*60*24*5 && $paidTime<=$todayEnd-60*60*24*5):
                $theseDayBooking[5] += 1;
                break;
            case ($paidTime>=$todayBegin-60*60*24*6 && $paidTime<=$todayEnd-60*60*24*6):
                $theseDayBooking[6] += 1;
                break;
            default :
                break;
        }
    }
}
//生成产看今日预订的findText
$findText1 = '';
foreach ($itemData1 as $key => $value) {
    if($key=='paidTime') $value = '>='.$todayBegin;
    $firstChar = substr($value, 0, 1);
    $condition = ($firstChar == '=' || $firstChar == '>'|| $firstChar == '<') ? $key.$value : $key.'='.$value;
    $findText1 = $findText1 == '' ? $condition : $findText1.' '.$condition;
}

/*
 * 总预订数
 */
$view2 = $cart->getView(array('itemData' => array('paymentStatus' => BCart::Payment_Status_Paid)));
$allBooking = count($view2);

/*
 * 读出今日剩余场地数量
 */
$itemData2 = array(
    'city' => 'china.chongqing',
    'bookingStatus' => 'available',
    'startTime' => '>='.$todayBegin,
    'endTime' => '<='.$todayEnd
);
$findText2 = '';
foreach ($itemData2 as $key => $value) {
    $firstChar = substr($value, 0, 1);
    $condition = ($firstChar == '=' || $firstChar == '>'|| $firstChar == '<') ? $key.$value : $key.'='.$value;
    $findText2 = $findText2 == '' ? $condition : $findText2.' '.$condition;
}
$treeTable = new BTreeTable('BWInputValue');
$valueList1 = $treeTable->getValueList('startTime', array('itemData' => $itemData2));
$todayRemain = 0;
foreach ($valueList1 as $count) {
    $todayRemain += $count;
}

/*
 * 读出今日已售场地数量
 */
$itemData3 = array(
    'city' => 'china.chongqing',
    'bookingStatus' => 'paid',
    'startTime' => '>='.$todayBegin,
    'endTime' => '<='.$todayEnd
);
$valueList2 = $treeTable->getValueList('startTime', array('itemData' => $itemData3));
$todaySold = 0;
foreach ($valueList2 as $count) {
    $todaySold += $count;
}

/*
 * 读出所有的剩余场地数目
 */
$valueList3 = $treeTable->getValueList('startTime', array('itemData' => array('city'=>'china.chongqing','bookingStatus'=>'available')));
$allRemain = 0;
foreach ($valueList3 as $count) {
    $allRemain += $count;
}
?>
<meta name="viewport" contdent="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1" media="(device-height: 568px)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="full-screen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl.'/js/mobile/Chart.min.js'?>"></script>
<style>
    .color_link {color:#FDB338;}
    body{background-color:#F5F5F5;}
    .articleTitle i {display: block;width: 4px;height: 15px;background-color: #4FA413;float: left;}
    .articleTitle b {font-size: 17px;line-height: 17px;font-weight: bold;color: #4fa413;padding-left: 12px;}
    .articleList{margin:12px 8px 2px;background-color:#fff;border:1px solid #E5E5E5;padding:9px 14px 0px;}
    .articleList table{font-weight:normal;width:100%;margin-bottom: 9px;}
    hr{border:1px dashed #dedede;margin:0;}
    .right_link{text-align:right;}
    .pie_span1{display:inline-block;width:20px;height:10px;background-color:#5AD3D1;}
    .pie_span2{display:inline-block;width:20px;height:10px;background-color:#FF5A5E;}
    .num_style1{color:#FDB338;font-size:16px;font-weight:bold}
    .num_style2{color:#2bb2a3;font-size:16px;font-weight:bold}
    .gray_word{font-size:12px;color:gray;vertical-align:top;line-height:12px;}
</style>
<body id="index">
    <header class="navbar" style="min-height:52px;">
        <table style="width:100%;">
            <tr>
                <td style="width:10%;text-align:left;"></td>
                <td style="width:80%;text-align:center;"><h4 style="margin-top:15px;">管理预订</h4></td>
                <td style="width:10%;text-align:right;">
                    <div style="margin-right:10px;"><?php
                        echo $control->getAjaxLink(array(
                            'name' => '<img src="'.Yii::app()->request->baseUrl.'/images/mobile/refresh.png'.'" width="20" height="20"/>', 
                            'id' => 'manageBooking'), array(
                            'htmlOptions' => array(
                            'enctype' => 'multipart/form-data'
                        )));?>
                    </div>
                </td>
            </tr>
        </table>
        <div id="ajax-loaderdiv" style="position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 35000;background-color: rgba(0, 0, 0, 0.3);display: none;">
            <img src="/billyn-dev/images/ajax-loader.gif" class="ajax-loader"style="position: absolute;left: 50%;top: 50%;margin-left: -16px;margin-top: -16px;display: block;"/>
        </div>
    </header>
    <h4 class="articleTitle clears">
        <i></i>
        <b>销售情况</b>
    </h4>
    <div class="articleList">
        <table>
            <tr>
                <td>今日预订数：<font class="num_style2"><?php echo $theseDayBooking[0];?></font></td>
                <td class="right_link"><?php
                    echo $control->getAjaxLink(array(
                        'name' => '查看>>', 
                        'id' => 'manageBooking_bookingList'), array(
                        'flagText' => '今日预订',
                        'findText' => $findText1,
                        'htmlOptions' => array('class' => '')
                    ));?>
                </td>
            </tr>
        </table>
    </div>
    <div class="articleList">
        <table>
            <tr>
                <td>近日预订查看：</td>
            </tr>
        </table>
        <hr/>
        <table>
            <tr>
                <td>
                    <br/>
                    <canvas id="chart-line" width="300" height="200"></canvas>
                </td>
            </tr>
        </table>
    </div>
    <br/>
    <h4 class="articleTitle clears">
        <i></i>
        <b>场地情况</b>
    </h4>
    <div class="articleList">
        <table>
            <tr>
                <td>今日剩余场地数：<font class="num_style2"><?php echo $todayRemain;?></font></td>
                <td class="right_link"><?php
                    echo $control->getAjaxLink(array(
                        'name' => '查看>>', 
                        'id' => 'manageBooking_remainList'), array(
                        'flagText' => '今日剩余场地',
                        'findText' => $findText2,
                        'htmlOptions' => array('class' => '')
                    ));?>
                </td>
            </tr>
        </table>
        <hr/>
        <table>
            <tr>
                <td>
                    <br/>
                    <canvas id="chart-pie" width="180" height="180"></canvas>
                </td>
                <td class="gray_word">
                    <br/><br/>
                    <span class="pie_span1"></span> 已售场地：<font class="num_style1"><?php echo $todaySold;?></font>
                    <br/><br/>
                    <span class="pie_span2"></span> 剩余场地：<font class="num_style1"><?php echo $todayRemain;?></font>
                </td>
            </tr>
        </table>
    </div>
    <div class="articleList">
        <table>
            <tr>
                <td>总预订数量：<font class="num_style2"><?php echo $allBooking;?></font></td>
            </tr>
        </table>
        <table style="padding-top:9px;">
            <tr>
                <td>总剩余数量：<font class="num_style2"><?php echo $allRemain;?></font></td>
            </tr>
        </table>
    </div>
    <script>
        var pieData = [
            {
                value: <?php echo $todayRemain;?>,
                color:"#F7464A",
                highlight: "#FF5A5E",
                label: "Red"
            },
            {
                value: <?php echo $todaySold;?>,
                color: "#46BFBD",
                highlight: "#5AD3D1",
                label: "Green"
            }
        ];
        var data = {
            labels: [<?php 
                    foreach ($theseDayBooking as $key => $value) {
                        if($key == 0) {?>
                            "<?php echo '今天';?>"<?php
                        } else {?>
                            "<?php echo date('d',$todayBegin-60*60*24*$key).'号';?>",<?php
                        }
                    }?>],
            datasets: [
                {
                    label: "My Second dataset",
                    fillColor: "rgba(151,187,205,0.2)",
                    strokeColor: "rgba(151,187,205,1)",
                    pointColor: "rgba(151,187,205,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(151,187,205,1)",
                    data: [<?php 
                            foreach ($theseDayBooking as $key => $value) {
                                if($key == 0) {
                                    echo $value;
                                } else {
                                    echo $value.',';
                                }
                            }?>]
                }
            ]
        };
        function addChart(){
            var ctx = document.getElementById("chart-pie").getContext("2d");
            var myPie = new Chart(ctx).Pie(pieData,{
                segmentShowStroke : true
            });

            var ctx1 = document.getElementById("chart-line").getContext("2d");
            var myLineChart = new Chart(ctx1).Line(data, {
                segmentShowStroke : true
            });
        };
        addChart();
    </script>
    <footer>
        <div class="footer-copyright">
            <div class="hr"></div>
            <span class="footer-copyright-text">
                Copyright © 2014 billyn.net </span>
        </div>
    </footer>
</body>